package com.management.config;

import com.management.security.LoginErrorHandler;
import com.management.security.LoginSuccessHandler;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.channel.ChannelProcessingFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.annotation.Resource;


@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{


    @Resource
    private LoginSuccessHandler loginSuccessHandler;

    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource cfs = new UrlBasedCorsConfigurationSource();
        CorsConfiguration cors = new CorsConfiguration();
        cors.addAllowedOriginPattern("*");
        cors.addAllowedMethod("*");
        cors.setAllowCredentials(true);
        cors.addAllowedHeader("*");
        cfs.registerCorsConfiguration("/**", cors);
        CorsFilter cf = new CorsFilter(cfs);
        return cf;
    }

    @Bean
    public PasswordEncoder createPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterBefore(corsFilter(), ChannelProcessingFilter.class).
                httpBasic().disable()
            .csrf().disable();
        http.addFilterBefore(corsFilter(), ChannelProcessingFilter.class).
                httpBasic().disable()
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/account/loginerror","/login","/attach/uploadFill",
                        "/swagger-ui.html","/v2/api-docs","/webjars/**",
                        "/swagger-resources/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
              .loginPage("/account/loginerror")
                .loginProcessingUrl("/login")
                .successHandler(loginSuccessHandler)
                .failureHandler(new LoginErrorHandler());

    }

    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        configuration.setAllowCredentials(true);
        configuration.addAllowedOriginPattern("*");
        configuration.addAllowedHeader(CorsConfiguration.ALL);
        configuration.addAllowedMethod(CorsConfiguration.ALL);
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}
